package com.muqi.app.qlearn.login;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.NetWorkUtils;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.AppUtils;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.R;

public class ForgetPwdActivity extends BaseFragmentActivity implements
		OnClickListener, OnCheckedChangeListener {
	private EditText phone_number;
	private EditText check_code;
	private EditText password;
	private EditText repassword;
	private Button btn_register;

	private TextView get_check_code;

	private String strPhone_number;
	private String strPassword;
	private String strRePassword;
	private String strCheckcode;
	private String confirm_code = "";

	private int type = 2; /*1 代表家长修改   2 代表学生修改 */

	private CheckBox mScb;
	private CheckBox mPcb;
	private RelativeLayout mRCode;

	private CountDownTimer timer = new CountDownTimer(60000, 1000) {

		@Override
		public void onTick(long millisUntilFinished) {
			get_check_code.setEnabled(false);
			get_check_code.setText((millisUntilFinished / 1000) + "秒后重发");
		}

		@Override
		public void onFinish() {
			get_check_code.setEnabled(true);
			get_check_code.setText("重新获取");
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aty_forget_pwd);
	}

	@Override
	protected void onInit() {
		phone_number = (EditText) findViewById(R.id.phone_number);
		check_code = (EditText) findViewById(R.id.check_code_input);
		password = (EditText) findViewById(R.id.password_input);
		repassword = (EditText) findViewById(R.id.confirm_input);
		btn_register = (Button) findViewById(R.id.btn_register_now);
		btn_register.setOnClickListener(this);
		get_check_code = (TextView) findViewById(R.id.get_check_code);
		get_check_code.setOnClickListener(this);

		mScb = (CheckBox) findViewById(R.id.cb_s);
		mPcb = (CheckBox) findViewById(R.id.cb_p);
		mRCode=(RelativeLayout) findViewById(R.id.rl_code);
		mScb.setOnCheckedChangeListener(this);
		mPcb.setOnCheckedChangeListener(this);
	}

	@Override
	protected void onData() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_register_now:
			strPhone_number = phone_number.getText().toString().trim();
			strPassword = password.getText().toString().trim();
			strRePassword = repassword.getText().toString().trim();// 获取输入的确认密码
			strCheckcode = check_code.getText().toString().trim();
			if (checkInput(type)) {
				updatePassWord();
			}
			break;
		case R.id.get_check_code:
			strPhone_number = phone_number.getText().toString().trim();
			if (TextUtils.isEmpty(strPhone_number)) {
				phone_number.setError(getString(R.string.phone_empty));
			} else {
				getCheckcode();
			}
			break;

		default:
			break;
		}
	}
     /**
      *  修改密码
      */
	private void updatePassWord() {
		if (NetWorkUtils.isNetworkAvailable(this)) {
			showProgressWithState("正在保存...");
			HashMap<String, Object> register = new HashMap<String, Object>();
			register.put("user_name", strPhone_number);
			register.put("mobile", strPhone_number);
			register.put("newpassword", strPassword);
			String requestUrl="";
			if(type==1){
				 requestUrl = ParamsUtils.buildParams(
						NetWorkApi.Forget_Pwd_Api, register);
			}else if(type==2){
				 requestUrl = ParamsUtils.buildParams(
							NetWorkApi.Forget_Spwd_Api, register);
			}
			
			MyAsyncHttp.get(mContext, requestUrl, new AsyncCallBackListener() {

				@Override
				public void onAsyncCallBack(RESULTCODE resuleCode,
						String request, String result) {
					Log.e("===", result);
					hidingSVloading();
					try {
						JSONObject resultJson = new JSONObject(result);
						int errcode = resultJson.getInt("errcode");
						String errdesc = resultJson.getString("errdesc");
						if (errcode == 0) {
							finish();
							ShowToast.showShort(mContext, errdesc);
						} else {
							ShowToast.showShort(mContext, errdesc);
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}

				}
			});

		} else {
			ShowToast.showShort(this, R.string.net_break);// 提示无网络
		}
		
	}

	
	/**
	 * 获取验证码
	 */
	public void getCheckcode() {
		if (NetWorkUtils.isNetworkAvailable(this)) {
			timer.start();
			HashMap<String, Object> register = new HashMap<String, Object>();
			register.put("mobile", strPhone_number);
			String requestUrl = ParamsUtils.buildParams(
					NetWorkApi.CheckCode_Api, register);
			MyAsyncHttp.get(mContext, requestUrl, new AsyncCallBackListener() {

				@Override
				public void onAsyncCallBack(RESULTCODE resuleCode,
						String request, String result) {
					try {
						JSONObject resultJson = new JSONObject(result);
						confirm_code = resultJson.getString("data");
					} catch (JSONException e) {
						e.printStackTrace();
					}

				}
			});

		} else {
			ShowToast.showShort(this, R.string.net_break);// 提示无网络
		}
	}

	/**
	 * 检测用户名和密码
	 * 
	 * @return
	 */
	private boolean checkInput(int type) {
		switch (type) {
		case 1:
			if (TextUtils.isEmpty(strPhone_number)) {
				phone_number.setError(getString(R.string.phone_empty));
				return false;
			} else if (!AppUtils.checkPhone(strPhone_number)) {
				phone_number.setError("手机号输入不正确");
				return false;
			}
			// 验证码非空验证
			else if (TextUtils.isEmpty(strCheckcode)) {
				check_code.setError(getString(R.string.checkcode_empty));
				return false;
			}
			// 验证码正确性验证
			else if (!confirm_code.equals(strCheckcode)) {
				check_code.setError(getString(R.string.checkcode_wrong));
				return false;
			}
			break;
		case 2:
			if (TextUtils.isEmpty(strPhone_number)) {
				phone_number.setError("用户名不能为空");
				return false;
			} 
			break;
		}

		if (TextUtils.isEmpty(strPassword)) {
			password.setError(getString(R.string.login_password_empty));
		} else if (strPassword.trim().length() < 6
				|| strPassword.trim().length() > 20) {
			password.setError(getString(R.string.password_length_alert));
		}
		// 验证输入的确认密码
		else if (TextUtils.isEmpty(strRePassword)) {
			repassword.setError(getString(R.string.register_repassword_empty));
		}
		// 判断两次密码是否输入一致
		else if (!strPassword.equals(strRePassword)) {
			repassword.setError(getString(R.string.password_different));
		} else {
			return true;
		}
		return false;
	}

	public void onBack(View v) {
		this.finish();
	}

	@Override
	public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
		switch (arg0.getId()) {
		case R.id.cb_s:
			if (arg1) {
				type=2;
				mPcb.setChecked(false);
				
				phone_number.setHint("请输入用户名");
				mRCode.setVisibility(View.GONE);
				
			}
			break;
		case R.id.cb_p:
			if (arg1) {
				type=1;
				phone_number.setHint("请输入手机号");
				mScb.setChecked(false);
				mRCode.setVisibility(View.VISIBLE);
				
			}
			break;
		default:
			break;
		}

	}
}
